﻿using agsXMPP;
using agsXMPP.protocol.client;
using JBotAPI.Logs;
using JClient.Contacts;
using System.ComponentModel;
using System.Windows.Threading;

namespace JClient {
	public partial class ConnectionProvider {
		private Dispatcher Dispatcher;

		public ConnectionProvider( ) {
			this.Dispatcher = Dispatcher.CurrentDispatcher;

			this.Client = new XmppClientConnection( );
			this.Client.OnAgentEnd += new agsXMPP.ObjectHandler(Client_OnAgentEnd);
			this.Client.OnAgentItem += new agsXMPP.XmppClientConnection.AgentHandler(Client_OnAgentItem);
			this.Client.OnAgentStart += new agsXMPP.ObjectHandler(Client_OnAgentStart);
			this.Client.OnAuthError += new agsXMPP.XmppElementHandler(Client_OnAuthError);
			this.Client.OnBinded += new agsXMPP.ObjectHandler(Client_OnBinded);
			this.Client.OnClose += new agsXMPP.ObjectHandler(Client_OnClose);
			this.Client.OnError += new agsXMPP.ErrorHandler(Client_OnError);
			this.Client.OnIq += new agsXMPP.protocol.client.IqHandler(Client_OnIq);
			this.Client.OnLogin += new agsXMPP.ObjectHandler(Client_OnLogin);
			this.Client.OnMessage += new agsXMPP.protocol.client.MessageHandler(Client_OnMessage);
			this.Client.OnPasswordChanged += new agsXMPP.ObjectHandler(Client_OnPasswordChanged);
			this.Client.OnPresence += new agsXMPP.protocol.client.PresenceHandler(Client_OnPresence);
			this.Client.OnReadSocketData += new agsXMPP.net.BaseSocket.OnSocketDataHandler(Client_OnReadSocketData);
			this.Client.OnReadXml += new agsXMPP.XmlHandler(Client_OnReadXml);
			this.Client.OnRegistered += new agsXMPP.ObjectHandler(Client_OnRegistered);
			this.Client.OnRegisterError += new agsXMPP.XmppElementHandler(Client_OnRegisterError);
			this.Client.OnRegisterInformation += new agsXMPP.protocol.iq.register.RegisterEventHandler(Client_OnRegisterInformation);
			this.Client.OnRosterEnd += new agsXMPP.ObjectHandler(Client_OnRosterEnd);
			this.Client.OnRosterItem += new agsXMPP.XmppClientConnection.RosterHandler(Client_OnRosterItem);
			this.Client.OnRosterStart += new agsXMPP.ObjectHandler(Client_OnRosterStart);
			this.Client.OnSaslEnd += new agsXMPP.ObjectHandler(Client_OnSaslEnd);
			this.Client.OnSaslStart += new agsXMPP.sasl.SaslEventHandler(Client_OnSaslStart);
			this.Client.OnSocketError += new agsXMPP.ErrorHandler(Client_OnSocketError);
			this.Client.OnStreamError += new agsXMPP.XmppElementHandler(Client_OnStreamError);
			this.Client.OnWriteSocketData += new agsXMPP.net.BaseSocket.OnSocketDataHandler(Client_OnWriteSocketData);
			this.Client.OnWriteXml += new agsXMPP.XmlHandler(Client_OnWriteXml);
			this.Client.OnXmppConnectionStateChanged += new agsXMPP.XmppConnectionStateHandler(Client_OnXmppConnectionStateChanged);
			//this.Client.OnXmppError += new XmppElementHandler(Client_OnXmppError);

			this.Me = new RosterContact( );
			this.Me.PropertyChanged += new PropertyChangedEventHandler(Me_PropertyChanged);

			this.Contacts = new RosterContactCollection(Client);
			this.Services = new RosterContactCollection(Client, true);

			this.Logs = new LogCollection(this);
		}

		private void Me_PropertyChanged(object sender, PropertyChangedEventArgs e) {
			if (!this.Dispatcher.CheckAccess( )) {
				this.Dispatcher.Invoke(new PropertyChangedEventHandler(Me_PropertyChanged), sender, e);
			} else {
				if (e.PropertyName == "StatusType") {
					this.presenceTransports( );
				}
			}
		}
		protected XmppClientConnection Client {
			get;
			set;
		}

		public RosterContact Me {
			get;
			protected set;
		}

		public RosterContactCollection Contacts {
			get;
			protected set;
		}
		public RosterContactCollection Services {
			get;
			protected set;
		}

		public LogCollection Logs {
			get;
			protected set;
		}

		public string Username {
			get {
				return this.Client.Username;
			}
			set {
				if (this.Me.Jid.User != value)
					this.Me.Jid.User = value;
				this.Client.Username = value;
			}
		}
		public string Server {
			get {
				return this.Client.Server;
			}
			set {
				if (this.Me.Jid.Server != value)
					this.Me.Jid.Server = value;
				this.Client.Server = value;
			}
		}
		public string Password {
			get {
				return this.Client.Password;
			}
			set {
				this.Client.Password = value;
			}
		}
		public int Port {
			get {
				return this.Client.Port;
			}
			set {
				this.Client.Port = value;
			}
		}
		public ShowType Show {
			get {
				return this.Client.Show;
			}
			set {
				if (this.Me.ShowType != value)
					Me.ShowType = value;
				this.Client.Show = value;
			}
		}
		public string Status {
			get {
				return this.Client.Status;
			}
			set {
				if (this.Me.Status != value)
					this.Me.Status = value;
				this.Client.Status = value;
			}
		}
		public bool RegisterAccount {
			get {
				return this.Client.RegisterAccount;
			}
			set {
				this.Client.RegisterAccount = value;
			}
		}
		public bool AutoResolveConnectServer {
			get {
				return this.Client.AutoResolveConnectServer;
			}
			set {
				this.Client.AutoResolveConnectServer = value;
			}
		}
		public bool AutoAgents {
			get {
				return this.Client.AutoAgents;
			}
			set {
				this.Client.AutoAgents = value;
			}
		}
		public bool AutoRoster {
			get {
				return this.Client.AutoRoster;
			}
			set {
				this.Client.AutoRoster = value;
			}
		}
		public bool KeepAlive {
			get {
				return this.Client.KeepAlive;
			}
			set {
				this.Client.KeepAlive = value;
			}
		}
		public int KeepAliveInterval {
			get {
				return this.Client.KeepAliveInterval;
			}
			set {
				this.Client.KeepAliveInterval = value;
			}
		}
		public bool UseSSL {
			get {
				return this.Client.UseSSL;
			}
			set {
				this.Client.UseSSL = value;
			}
		}
		public bool UseStartTLS {
			get {
				return this.Client.UseStartTLS;
			}
			set {
				this.Client.UseStartTLS = value;
			}
		}
		public int Priority {
			get {
				return this.Client.Priority;
			}
			set {
				this.Client.Priority = value;
			}
		}
		public string ClientVersion {
			get {
				return this.Client.ClientVersion;
			}
			set {
				this.Client.ClientVersion = value;
			}
		}
		public string Resource {
			get {
				return this.Client.Resource;
			}
			set {
				if (this.Me.Jid.Resource != value)
					this.Me.Jid.Resource = value;
				this.Client.Resource = value;
			}
		}

		public XmppConnectionState XmppConnectionState {
			get {
				return this.Client.XmppConnectionState;
			}
		}
	}
}